﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OAProject.Domain.Entities;
using OAProject.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace OAProject.API.Controllers
{
    /// <summary>
    ///  日志API控制器
    /// </summary>
    [Route("api/[controller]/[Action]")]
    [ApiController]
    [Authorize]     //全局授权过滤器
    public class NlogAPIController : ControllerBase
    {
        //依赖注入
        private INLogRepository _nLogRepository;               //日志表

        public NlogAPIController(INLogRepository nLogRepository)
        {
            _nLogRepository = nLogRepository;
        }

        /// <summary>
        ///  日志显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShow(bool state = false, int page = 1, int limit = 10)
        {
            List<LogMessage> roles = null;
            roles = _nLogRepository.Show().Where(x => x.State.Equals(state)).ToList();
            var list = roles.Skip((page - 1) * limit).Take(limit).ToList();
            
            return Ok(new
            {
                data = list,
                code = 0,
                msg = "",
                count = roles.Count
            });
        }

        /// <summary>
        ///  VUE日志显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Show(string operationName = "", int pageIndex=1,int pageSize=2)
        {
            var list = _nLogRepository.Show();
            var cha = list.Where(x => x.OperationName.Equals(operationName)).ToList();
            var data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Ok(new
            {
               rows= data,
               total= cha.Count
            });
        }

        /// <summary>
        /// Vue日志表删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Delete(int logId)
        {
            var list = _nLogRepository.Delete(logId);
            return Ok(list);
        }

        /// <summary>
        /// 日志的反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Fantian(int logId)
        {
            var list = _nLogRepository.GetById(logId);
            return Ok(new
            {
                data = list
            });
        }

        /// <summary>
        ///  日志的修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Update([FromForm] LogMessage obj)
        {
            obj.OperationTime = DateTime.Now;      //创建当前时间
            var list = _nLogRepository.Update(obj);
            return Ok(list);
        }
        /// <summary>
        /// 修改状态   如果状态为false时修改当时的状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UptFalse(string id)
        {
            int n = _nLogRepository.UptTrue(id);

            if (n > 0)
            {
                return Ok(0);
            }
            else
            {
                return Ok(1);
            }


        }

        /// <summary>
        /// 修改状态   如果状态为true时修改当时的状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UptTrue(string id)
        {
            int n = _nLogRepository.Upt(id);

            if (n > 0)
            {
                return Ok(0);
            }
            else
            {
                return Ok(1);
            }

        }
        /// <summary>
        /// 日志还原显示数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetHuan(bool state = true,int page = 1, int limit = 10)
        {

            List<LogMessage> roles = _nLogRepository.Show().Where(x => x.State.Equals(state)).ToList();

            var list = roles.Skip((page - 1) * limit).Take(limit).ToList();
            return Ok(new
            {
                data = list,
                code = 0,
                msg = "",
                count = roles.Count
            });
        }
    }
}
